import 'package:fluttertoast/fluttertoast.dart';
import 'package:macnov/common/credit_card.dart';
import 'package:macnov/models/company.dart';
import 'package:macnov/models/summary.dart';
import 'package:macnov/utils/Api.dart';
import 'package:macnov/utils/constant.dart';
import 'package:mobx/mobx.dart';

// Include generated file
part 'wallet.g.dart';

// This is the class used by rest of your codebase
class WalletStore = _WalletStore with _$WalletStore;

// The store-class
abstract class _WalletStore with Store {
  @observable
  PhysicalCardModel physicalCard;

  @observable
  AccountInfoModel accountInfo;

  @observable
  CreditInfoModel creditInfo;

  @observable
  List vcards = [];

  @observable
  List<MonthPaymentModel> monthlyDatas;

  @observable
  List<MonthPaymentCategoryModel> monthlyCategoriess;

  @observable
  bool allDataReady = false;

  @observable
  CreditCardViewModel authCardInfo;

  @observable
  bool loadAuthCardInfoReady = false;

  @action
  Future loadVCards() async {
    List<dynamic> allCards = await Api.loadVCards();
    vcards = allCards
        .map((vcardJson) => new CreditCardViewModel.fromJson(vcardJson))
        .toList();
  }

  @action
  checkAllDataReady() {
    if (monthlyDatas != null &&
        monthlyCategoriess != null &&
        accountInfo != null &&
        creditInfo != null) {
      allDataReady = true;
    }
  }

  @action
  Future loadPhysicalCard() async {
    var physicalCardJson = await Api.loadPhysicalCard();
    physicalCard = new PhysicalCardModel.fromJson(physicalCardJson);
  }

  @action
  Future loadOwnerCompanyAccountInfo() async {
    var accountInfoJson = await Api.loadOwnerCompanyAccountInfo();
    accountInfo = new AccountInfoModel.fromJson(accountInfoJson);
    checkAllDataReady();
  }

  @action
  Future getCreditInfo() async {
    var creditInfoJson = await Api.getCreditInfo();
    creditInfo = new CreditInfoModel.fromJson(creditInfoJson);
    checkAllDataReady();
  }

  @action
  Future getMonthlyPayment(int month) async {
    List<dynamic> jsonList = await Api.getMonthlyPayment("$month");
    monthlyDatas =
        jsonList.map((d) => new MonthPaymentModel.fromJson(d)).toList();
    checkAllDataReady();
  }

  @action
  Future getMonthlyPaymentCategory(int month) async {
    List<dynamic> jsonList = await Api.getMonthlyPaymentCategory("$month");

    monthlyCategoriess =
        jsonList.map((d) => new MonthPaymentCategoryModel.fromJson(d)).toList();
    var total = monthlyCategoriess.fold(0, (pre, cur) => pre + cur.amount);
    monthlyCategoriess.forEach(
      (item) => (item.percent = item.amount / total),
    );
    checkAllDataReady();
  }

  @action
  Future loadAuthCardInfo(CreditCardViewModel vcard) async {
    // var showCardData = await Api.showVcardAuth(vcard.id);
    // if (showCardData['success'] ?? false) {

    // }
    loadAuthCardInfoReady = false;
    var jsonCardInfo = await Api.getCardInfo(vcard.id);
    if (jsonCardInfo == null) return;
    CreditCardViewModel vcardData = CreditCardViewModel.fromJson(jsonCardInfo);
    authCardInfo = vcardData;
    loadAuthCardInfoReady = true;
  }

  Future addVCard(String name, OnVoidSuccess cb) async {
    Map<String, dynamic> json = await Api.addVirtualCard(name);
    if (json != null) {
      String message = 'Add Successfully !';
      if (false == json['success']) {
        message = json['message'] ?? '';
      } else {
        cb();
      }
      Fluttertoast.showToast(
          msg: message,
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
          timeInSecForIos: 1,
          fontSize: 16.0);
    }
  }

  Future delVCard(String cardId, OnVoidSuccess cb) async {
    Map<String, dynamic> json = await Api.deleteCard(cardId);
    if (json != null) {
      String message = 'Delete Successfully !';
      if (false == json['success']) {
        message = json['message'] ?? '';
      } else {
        cb();
      }
      Fluttertoast.showToast(
          msg: message,
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
          timeInSecForIos: 1,
          fontSize: 16.0);
    }
  }

  Future expireCard(String cardId, OnVoidSuccess cb) async {
    Map<String, dynamic> json = await Api.expireCard(cardId);
    if (json != null) {
      String message = 'Update Successfully !';
      if (false == json['success']) {
        message = json['message'] ?? '';
      } else {
        cb();
      }
      Fluttertoast.showToast(
          msg: message,
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
          timeInSecForIos: 1,
          fontSize: 16.0);
    }
  }

  Future activeCard(String cardId, OnVoidSuccess cb) async {
    Map<String, dynamic> json = await Api.activeCard(cardId);
    if (json != null) {
      String message = 'Update Successfully !';
      if (false == json['success']) {
        message = json['message'] ?? '';
      } else {
        cb();
      }
      Fluttertoast.showToast(
          msg: message,
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
          timeInSecForIos: 1,
          fontSize: 16.0);
    }
  }
}

final WalletStore walletStore = new WalletStore();
